package com.atguigu.guli.service.edu.controller.admin;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.query.TeacherQuery;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author uuei
 * @since 2021-07-11
 */
@Api(tags = "讲师管理")
@RestController
@RequestMapping("/admin/edu/teacher")
@Slf4j
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @ApiOperation("所有讲师列表")
    @GetMapping("list")
    public R listAll(){

        log.info("所有讲师列表....................");
        log.warn("所有讲师列表....................");

        List<Teacher> list = teacherService.list();
        return R.ok().data("items",list).message("获取讲师列表成功");
    }

    @ApiOperation(value = "根据ID删除讲师",notes = "根据ID删除讲师")
    @DeleteMapping("remove/{id}")
    public R removeById(
            @ApiParam(value = "讲师ID",required = true)
            @PathVariable String id){
        boolean b = teacherService.removeById(id);
        if (b){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("删除失败");
        }
    }

    @ApiOperation("分页讲师列表")
    @GetMapping("list/{page}/{limit}")
    public R listPage(
            @ApiParam(value = "当前页码",required = true) @PathVariable Long page,
            @ApiParam(value = "每页记录数",required = true) @PathVariable Long limit,
            @ApiParam(value = "讲师查询对象") TeacherQuery teacherQuery){

        IPage<Teacher> pageModel = teacherService.selectPage(page, limit, teacherQuery);
        return R.ok().data("pageModel",pageModel);
    }

    /**
     * 新增讲师
     */
    @ApiOperation("新增讲师")
    @PostMapping("save")
    public R save(
            @ApiParam(value = "讲师对象",required = true) @RequestBody Teacher teacher){
        boolean result = teacherService.save(teacher);
        if (result){
            return R.ok().message("新增成功");
        }else {
            return R.error().message("新增失败");
        }
    }

    /**
     * 根据id修改，更新
     */
    @ApiOperation("根据ID更新讲师信息")
    @PutMapping("update")
    public R updateById(
            @ApiParam(value = "讲师对象",required = true) @RequestBody Teacher teacher){
        boolean result = teacherService.updateById(teacher);
        if (result){
            return R.ok().message("更新成功");
        }else{
            return R.error().message("更新失败");
        }
    }

    /**
     * 根据id查询
     */
    @ApiOperation("根据ID查询讲师")
    @GetMapping("getById/{id}")
    public R getById(
            @ApiParam(value = "讲师ID",required = true)@PathVariable String id){
        Teacher teacher = teacherService.getById(id);
        if (teacher != null){
            return R.ok().data("item",teacher).message("查询成功");
        }else {
            return R.error().message("该ID不存在");
        }
    }
}


